package akbderby.model;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.Persistent;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.InverseModelListRef;
import org.slim3.datastore.Model;
import org.slim3.datastore.ModelRef;
import org.slim3.datastore.Sort;

import akbderby.meta.AnswerMeta;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query.SortDirection;

@Model(schemaVersion = 1)
public class UserAnswer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey = true)
    private Key key;

    private ModelRef<Quiz> quizRef = new ModelRef<Quiz>(Quiz.class);

    @Attribute(persistent = false)
    private InverseModelListRef<Answer, UserAnswer> answerListRef =
        new InverseModelListRef<Answer, UserAnswer>(
            Answer.class,
            AnswerMeta.get().userAnswerRef.getName(),
            this,
            new Sort(
                AnswerMeta.get().questionNo.getName(),
                SortDirection.ASCENDING));

    @Persistent
    private String userNm;

    @Persistent
    private int score = 0;

    @Persistent
    private Date startTime;

    @Persistent
    private Date endTime;

    @NotPersistent
    private int ranking = 0;

    /**
     * Returns the key.
     * 
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     * 
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        UserAnswer other = (UserAnswer) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }

    public ModelRef<Quiz> getQuizRef() {
        return quizRef;
    }

    public InverseModelListRef<Answer, UserAnswer> getAnswerListRef() {
        return answerListRef;
    }

    public void setUserNm(String userNm) {
        this.userNm = userNm;
    }

    public String getUserNm() {
        return userNm;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getScore() {
        return score;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setRanking(int ranking) {
        this.ranking = ranking;
    }

    public int getRanking() {
        return ranking;
    }
}
